package cn.wolfcode.rbac.web.controller.system_manage;

import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.system_manage.IPermissionService;
import cn.wolfcode.rbac.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @version 1.0
 * @Author:Cao Zhijianu0:05:37
 */
@Controller
public class PermissionController {

    @Autowired
    private IPermissionService iPermissionService;

    /**
     * 权限列表的显示
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/24 0024 21:06
     * @param model :
     * @return : java.lang.String
     */
    @RequestMapping("/permission/list")
    public String list(Model model, QueryObject queryObject){

        // 查询出数据
        PageResult result = iPermissionService.query(queryObject);
//        System.out.println(permissions);
        // 回显数据
        model.addAttribute("result",result);

        return "/permission/list";
    }

    /**
     * 删除权限
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/24 0024 21:06
     * @return : java.lang.String
     */
    @RequestMapping("/permission/delete")
    public String delete(Long id){
        iPermissionService.delete(id);

        // 重定向列表界面
        return "redirect:/permission/list";
    }

    /**
     * 新增和编辑权限
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/24 0024 21:14
     * @param id : 权限id
     * @return : java.lang.String
     */
    @RequestMapping("/permission/input")
    public String input(Model model, Long id){

        if(id != null){
            // 如果id不为空，则为编辑功能，编辑功能要回显数据
            // 根据权限id查找权限信息
            Permission permission = iPermissionService.get(id);
            model.addAttribute("entity",permission);
        }
        // 返回到编辑界面
        return "/permission/input";
    }

    /**
     * 新增和编辑权限的保存效果
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/24 0024 21:27
     * @param permission :
     * @return : java.lang.String
     */
    @RequestMapping("/permission/saveOrUpdate")
    public String saveOrUpdate(Permission permission){

        iPermissionService.saveOrUpdate(permission);

        return "redirect:/permission/list";
    }

    /**
     * 记载权限
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/25 0025 11:04
     * @return : java.lang.String
     */
    @RequestMapping("/permission/reload")
    public String reload(){
        iPermissionService.reload();
        return "redirect:/permission/list";
    }

}
